import { getToken, setToken, removeToken, setTimeStamp, getSid, setSid } from '@/utils/auth'
import { login } from '@/api/user'
import Cookies from 'js-cookie'
export default {
    namespaced: true,
    state: {
        // 存token
        userToken: getToken(),
        userSid: getSid()
    },
    mutations: {
        // 修改state
        // 存token
        setToken(state, token) {
            state.userToken = token
            setToken(state.userToken)
        },
        // 修改Sid
        // 存Sid
        setSid(state, sid) {
            state.userSid = sid
            setSid(state.userSid)
        },
        // 删除token以及sid
        delToken(state) {
            // state.userToken = null
            removeToken()
            // 同时删除sid
            Cookies.remove('sidkey')
        },
        // 退出登录
        logOut(state) {
            state.userToken = null
            removeToken()
            // 同时删除sid
            Cookies.remove('sidkey')
        }

    },
    actions: {
        // 登录
        async login(context, data) {
            const res = await login(data)
            // console.log('这里是请求登录信息', res)
            // 存储token以及sid
            context.commit('setToken', res.token)
            context.commit('setSid', res.sid)
            // 存时间戳
            setTimeStamp()
        }
    }
}